'use strict';

const {app, assert} = require('egg-mock/bootstrap');

describe('TopicApiTest', () => {
  let ctx,
    user,
    topic_id,
    topic,
    replyUser,
    mockUser;

  before(async function () {
    ctx = app.mockContext();
    mockUser = {id: '5c7cd4f781c857816114c940', access_token: 'edd8bcd1-db39-4ea8-8221-3849349eccc2'};

  });

  it('post /api/v1/topics should ok', async () => {
    const res = await app.httpRequest()
      .post('/api/v1/topics')
      .send({
        access_token: user.access_token,
        title: 'test title',
        tab: 'share',
        content: 'test content',
      })
      .expect(200);
    topic_id = res.body.topic_id;
  });

  it('post /api/v1/topics/update should ok', async () => {
    await app.httpRequest()
      .post('/api/v1/topics/update')
      .send({
        access_token: user.access_token,
        topic_id,
        title: 'update title',
        tab: 'share',
        content: 'update title',
      })
      .expect(200);
  });

  it('post /api/v1/topics/update should 404', async () => {
    await app.httpRequest()
      .post('/api/v1/topics/update')
      .send({
        access_token: user.access_token,
        topic_id: '012345678901234567890123',
        title: 'update title',
        tab: 'share',
        content: 'update title',
      })
      .expect(404);
  });

  it('post /api/v1/topics/update should 403', async () => {
    await app.httpRequest()
      .post('/api/v1/topics/update')
      .send({
        access_token: replyUser.access_token,
        topic_id,
        title: 'update title',
        tab: 'share',
        content: 'update title',
      })
      .expect(403);
  });

  it('get /topic/:id should ok', async () => {
    const res = await app.httpRequest()
      .get('/api/v1/topic/' + topic_id)
      .expect(200);
    const data = res.body.data;
    assert(data.id === topic_id);
    assert(data.content.includes('<div class="markdown-text">'));
  });

  it('get /topic/:id markdown should ok', async () => {
    const res = await app.httpRequest()
      .get('/api/v1/topic/' + topic_id)
      .query({mdrender: false})
      .expect(200);
    const data = res.body.data;
    assert(data.id === topic_id);
    assert(!data.content.includes('<div class="markdown-text">'));
    assert(!data.is_collect);
  });

  it('get /topic/:id with access_token should ok', async () => {
    const res = await app.httpRequest()
      .get('/api/v1/topic/' + topic.id)
      .query({access_token: user.access_token})
      .expect(200);
    const data = res.body.data;
    assert(data.is_collect);
    assert(data.replies[0].is_uped);
  });

  it('get /topic/:id should 404', async () => {
    await app.httpRequest()
      .get('/api/v1/topic/012345678901234567890123')
      .expect(404);
  });

  it('get /topics should ok', async () => {
    const res = await app.httpRequest()
      .get('/api/v1/topics')
      .expect(200);
    const data = res.body.data;
    assert(data.some(item => {
      return item.id === topic_id;
    }));
    assert(data.every(item => {
      return item.content.includes('<div class="markdown-text">');
    }));
  });

  it('get /topics with limit 2 should ok', async () => {
    const res = await app.httpRequest()
      .get('/api/v1/topics')
      .query({limit: 2})
      .expect(200);
    const data = res.body.data;
    assert(data.length === 2);
  });

  it('get /topics markdown should ok', async () => {
    const res = await app.httpRequest()
      .get('/api/v1/topics')
      .query({mdrender: false})
      .expect(200);
    const data = res.body.data;
    assert(data.some(item => {
      return item.id === topic_id;
    }));
    assert(data.every(item => {
      return !item.content.includes('<div class="markdown-text">');
    }));
  });


  it('createOriginalTag', async () => {
    const data = {
      access_token: mockUser.access_token,
      name: 'abcd'
    }

    const res = await app.httpRequest()
      .post('/api/v1/topic/createOriginalTag')
      .send(data)
      .expect(200);

    console.log(JSON.stringify(res));
    assert(res.body.status == 'success');
  });

});
